// pages/profile/index.ts


Page({
  onLoad() {
    this.setData({ ...getApp().userProfile })
  },
  getUserNikename(ev) {
    this.updateNikename(ev.detail.value)
  },
  async updateNikename(nickName) {
    if (nickName === '') return
    const { code } = await wx.http.put('/userInfo', { nickName })
    if (code !== 10000) return wx.utils.toast('用户信息更改失败')
  },
  getUserAvatar(ev) {
    this.updateAvatar(ev.detail.avatarUrl)
  },
  updateAvatar(avatar) {
    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath: avatar,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + getApp().token
      },
      formData: {
        type: 'avatar'
      },
      success: (result) => {
        const data = JSON.parse(result.data)
        if (data.code !== 10000) return wx.utils.toast('头像更新失败')
        this.setData({ avatar: data.data.url })
      }
    })
  }
})